package com.aitoedu.server.controller.student;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.util.SaResult;
import com.aitoedu.server.constant.MessageConstant;
import com.aitoedu.server.dto.AIReceiveDTO;
import com.aitoedu.server.dto.CreateConversationDTO;
import com.aitoedu.server.mapper.TitleMapper;
import com.aitoedu.server.service.ConversationService;
import com.aitoedu.server.service.TitleService;
import com.aitoedu.server.vo.OnlyTitleVO;
import com.aitoedu.server.vo.TitleVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student/AI")
@Slf4j
@CrossOrigin
@Tag(name = "AI相关接口")
@SaCheckRole("学生")
public class AIController {

    @Autowired
    private ConversationService conversationService;

    @Autowired
    private TitleMapper titleMapper;

    @Autowired
    private TitleService titleService;

    @GetMapping("/allConversation")
    @Operation(summary = "查询全部会话，每个会话和会话中的消息按照从新到旧排序")
    public SaResult allConversation(){
        List<TitleVO> list = conversationService.allConversation();
        return SaResult.ok().setData(list);
    }

    @GetMapping("/save")
    @Operation(summary = "收藏会话")
    public SaResult save(Long titleId){
        return conversationService.save(titleId);
    }

    @GetMapping("/onlyConversation")
    @Operation(summary = "查询单次会话，会话中的消息按照从新到旧排序")
    public SaResult onlyConversation(Long titleId){
        OnlyTitleVO onlyTitleVO = conversationService.onlyConversation(titleId);
        return SaResult.ok().setData(onlyTitleVO);
    }

    @PostMapping("/receive")
    @Operation(summary = "接收信息")
    public SaResult receive(@RequestBody AIReceiveDTO aiReceiveDTO){
        if(aiReceiveDTO.getContent() == null || aiReceiveDTO.getContent().isEmpty()) return SaResult.error("请输入问题");
        String AIReply = conversationService.receive(aiReceiveDTO);
        return SaResult.ok().setData(AIReply);
    }

    @PostMapping("/send")
    @Operation(summary = "发送信息")
    public String send(@RequestBody AIReceiveDTO aiReceiveDTO) {
        return conversationService.send(aiReceiveDTO);
    }

    @GetMapping("/statistics")
    @Operation(summary = "日统计信息")
    public SaResult statistics(){
        return SaResult.ok().setData(conversationService.statistics());
    }

    @GetMapping("/nowTitleId")
    @Operation(summary = "获取创建会话应使用的id")
    public SaResult nowTitleId(){
        return SaResult.ok().setData(titleMapper.nowTitleId()+1);
    }

    @PostMapping("/createConversation")
    @Operation(summary = "创建会话")
    public SaResult createConversation(@RequestBody CreateConversationDTO createConversationDTO){
        String ID = titleService.createConversation(createConversationDTO);
        return SaResult.ok(MessageConstant.CREATE_SUCCESS).setData(ID);
    }

    @GetMapping("/deleteConversation")
    @Operation(summary = "删除会话")
    public SaResult deleteConversation(Long titleId){
        titleService.deleteConversation(titleId);
        return SaResult.ok(MessageConstant.DELETE_SUCCESS);
    }

}
